#include "GameWorld.h"

GameWorld::GameWorld() : m_pPlayer(NULL), m_pCurrentRoom(NULL)
{
	//Creates a new Player object
	m_pPlayer		= new cPlayer(Vector3D(320, 200, 0.0f), true);

	//Creates a new Rock object
	m_pRock			= new cRock(Vector3D(320, 400, 0.0f));

	m_pCurrentRoom	= new Room(0, 0);
	m_pContactTest	= new cOverlapTest();

	//Controls the interface information
	m_bHasContactedX = false;
	m_bHasContactedY = false;

	//Information for controlling the list information
	m_enemyList.clear();
	m_projectileList.clear();
	m_objectList.clear();
	m_boundList.clear();

	//Game Condition Initialization
	m_bIsGameOver	= false;
	m_bIsGameWin	= false;
}

GameWorld::~GameWorld()
{
	delete m_pPlayer;

	delete m_pEFactory;
	delete m_pOFactory;
	delete m_pIFactory;

	delete m_pRock;
	delete m_pCurrentRoom;
	delete m_pContactTest;

	m_enemyList.clear();
	m_projectileList.clear();
	m_objectList.clear();
	m_boundList.clear();
}

void GameWorld::initWorld()
{
	//Initialize the level

	//Initialize the Enemy Factory
	m_pEFactory		= new cEnemyFactory();

	//Initialize the Obstacle Factory
	m_pOFactory		= new cObstacleFactory();

	//Initialize the Item Factory
	m_pIFactory		= new cItemFactory();
}

void GameWorld::updateWorld(float dt)
{
	//Controls the time variable for the update
	static float tickTime;

	//Increments the time variable in order to perform the test
	tickTime += dt;

	//Run the world update loop here
	if(tickTime >= TIME_TO_UPDATE)
	{
		//Update the player
		m_pPlayer->updatePlayer();

		//Update the projectiles fired into the world
		moveProjectiles();

		//Update the enemies in the world
		moveEnemies();

		//Update the objects in the world
		moveObjects();

		//Check to see if enemies need to be spawned
		SpawnEnemies();

		//Check to see if obstacles need to be spawned
		
		//Check to see if items need to be spawned

		//Run the collision detection system with the objects
		//that are in the game world
		SpatialTesting();
	}
}

void GameWorld::moveProjectiles()
{
}

void GameWorld::moveObjects()
{
}

void GameWorld::moveEnemies()
{
}

void GameWorld::SpatialTesting()
{
	//Testing collision with a rock
	if(m_pContactTest->AABBToAABB(m_pPlayer->getBound(), m_pRock->getAABB()))
	{
		//Controls the information for the player and rock position
		AABB playerBound	= m_pPlayer->getBound();
		AABB rockBound		= m_pRock->getBound();

		//First: Find the point of contact
		float		contactPointXR		= (playerBound.center.getX() + playerBound.radius.getX()) - (rockBound.center.getX() + rockBound.radius.getX());
		float		contactPointYU		= (playerBound.center.getY() + playerBound.radius.getY()) - (rockBound.center.getY() + rockBound.radius.getY());

		//Second: Find the direct of contact
		float		contactDirectionYU	= (float)atan(contactPointXR);
		float		contactDirectionXR	= (float)atan(contactPointYU);

		//Third:
		if(contactDirectionYU > 0.001f)
		{
			m_pPlayer->m_bMoveUp	= false;
		} 

		if(contactDirectionXR > 0.001f)
		{
			m_pPlayer->m_bMoveRight	= false;
		}
	}
	else
	{
		m_pPlayer->m_bMoveLeft		= true;
		m_pPlayer->m_bMoveRight		= true;
		m_pPlayer->m_bMoveDown		= true;
		m_pPlayer->m_bMoveUp		= true;
	}
}

void GameWorld::SpawnEnemies()
{
}

void GameWorld::SpawnObjects()
{
}

bool GameWorld::isTouchingCrypt()
{
	return false;
}

void GameWorld::PickupItem()
{
}

void GameWorld::DropItem()
{
}

void GameWorld::DestroyTombstone()
{
}

void GameWorld::OpenCrypt()
{
}

void GameWorld::ReleaseItemFromCrypt()
{
}

void GameWorld::TouchFountain()
{
}

void GameWorld::changeRoom()
{
}

void GameWorld::nextLevel()
{
}

bool GameWorld::isGameOver()
{
	return false;
}

bool GameWorld::isGameWin()
{
	return false;
}